package com.the_ring.chapter08.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.the_ring.chapter08.R;
import com.the_ring.chapter08.adapter.ImageAdapter;

import java.util.ArrayList;
import java.util.List;

public class BannerPager extends RelativeLayout {

    private Context mContext;
    private ViewPager bannerVp;
    private RadioGroup indicatorRg;

    public BannerPager(Context context) {
        this(context, null);
    }

    public BannerPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    // 初始化视图
    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.banner_pager, null);
        bannerVp = view.findViewById(R.id.vp_banner);
        indicatorRg = view.findViewById(R.id.rg_indicator);
        addView(view);
    }

    // 声明一个图像视图列表
    private List<ImageView> imageViews = new ArrayList<>();

    // 设置广告图片列表
    public void setImageViews(List<Integer> imageList) {
        int dip_15 = dip2px(mContext, 15);
        // 根据图片列表生成图像视图列表
        for (int i = 0; i < imageList.size(); i++) {
            Integer imageResId = imageList.get(i);
            ImageView iv = new ImageView(mContext);
            iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imageResId);
            imageViews.add(iv);
        }
        // 设置翻页视图的图像适配器
        bannerVp.setAdapter(new ImageAdapter(mContext, imageViews));
        // 给翻页视图添加简单的页面变更监听器，此时需要重写 onPageSelected 方法
        bannerVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mHandler.removeCallbacks(scroll);
                setSelectedButton(position); // 高亮显示该位置的指示按钮
                start();
            }
        });
        // 根据图片列表生成指示按钮列表
        for (int i = 0; i < imageList.size(); i++) {
            RadioButton radio = new RadioButton(mContext); // 创建一个单选按钮对象
            radio.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//            radio.setButtonDrawable(R.drawable.indicator_selector);
//            radio.setClickable(false);
            indicatorRg.addView(radio);
        }
        bannerVp.setCurrentItem(0);
        setSelectedButton(0);

        indicatorRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int count = group.getChildCount();
                for (int i = 0; i < count; i++) {
                    RadioButton radioButton = (RadioButton) group.getChildAt(i);
                    if (radioButton.isChecked()) {
                        bannerVp.setCurrentItem(i, false);
                    }
                }
            }
        });
    }

    private void setSelectedButton(int position) {
        ((RadioButton) indicatorRg.getChildAt(position)).setChecked(true);
    }

    // 根据是手机的分辨率从 dp 的单位转化为 px
    public int dip2px(Context context, float dpValue) {
        // 获取手机像素密度（1dp 对应几个 px）
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f); // 四舍五入取整
    }

    private int interval = 2000;
    // 开始广播
    public void start() {
        mHandler.postDelayed(scroll, interval);
    }

    private Handler mHandler = new Handler();
    private Runnable scroll = new Runnable() {
        @Override
        public void run() {
            int index = bannerVp.getCurrentItem() + 1;
            if (index >= imageViews.size()) {
                index = 0;
            }
            bannerVp.setCurrentItem(index);
            mHandler.postDelayed(this, interval);
        }
    };
}
